package com.hnair.bot.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hnair.bot.common.utils.BuildTree;
import com.hnair.bot.common.utils.MD5Util;
import com.hnair.bot.model.BfOrg;
import com.hnair.bot.model.BfUser;
import com.hnair.bot.model.TreeNode;
import com.hnair.bot.service.IBfOrgService;
import com.hnair.bot.service.IBfUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
public class LoginController {
	@Autowired
	private IBfOrgService orgService;
	
/*	@Autowired
	private IBfAreaService areaService;*/
	
	@Autowired
	private IBfUserService userService;
	
	/*@Autowired
	private IBfCompanyService iBfCompanyService;*/

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(BfUser user, HttpSession session,
			HttpServletRequest request) {
		ModelAndView modelView = new ModelAndView();
		modelView.setViewName("/login");
		return modelView;
	}

	@RequestMapping(value = "/submitLogin", method = RequestMethod.POST)
	public ModelAndView submitLogin(HttpSession session,
			HttpServletRequest request, BfUser user) {
		ModelAndView modelView = new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		
		System.out.println(MD5Util.getMD5String( user.getUserpasswd()));
		UsernamePasswordToken token = new UsernamePasswordToken(
				user.getUsername(), MD5Util.getMD5String( user.getUserpasswd()));
		token.setRememberMe(true);
		try {
			currentUser.login(token);
		} catch (Exception ace) {
			token.clear();
			request.getSession().setAttribute("shiroLoginFailure",
					"userOrPasswdError");
			modelView.setViewName("redirect:/login.do");
			return modelView;
		} 
		if (currentUser.isAuthenticated()) {
			
//			ParkUser searchUser = new ParkUser();
//			searchUser.setUsername(token.getUsername());
//		    List<ParkUser> userList = userService.login(searchUser);		    
//		    ParkUser userSession = userList.get(0);
			
			Wrapper<BfUser> wrapper=new EntityWrapper<BfUser>();
			wrapper.eq("username", user.getUsername());
			
			List<BfUser> userList1 = userService.selectList(wrapper);
			BfUser userSession = userList1.get(0);
		    
//			//添加组织机构id
//			List<Long> orgList=orgService.selectByPid(userSession.getOrgId());			
//			List<Long> orgIdList = new ArrayList<Long>();			
//			orgIdList.add(userSession.getOrgId());
//			if(null!= orgList){
//				orgIdList.addAll(orgList);
//			}			
//			
//			userSession.setOrgIdList(orgIdList);
			
			//添加组织机构(树形)	
//			BfOrg BfOrg = orgService.selectById(userSession.getOrgId());
//			List<TreeNode> trees = new ArrayList<TreeNode>();
//			TreeNode tree = new TreeNode();
//		    tree.setId(BfOrg.getId()+"");
//		    tree.setParentId(BfOrg.getPid()+"");
//		    tree.setText(BfOrg.getName());
//		    tree.setChildren(orgService.getOrgTreeByPidOrg(tree));    
//		    trees.add(tree);
//            List<TreeNode> treeNode = BuildTree.build(trees);
//
//			userSession.setOrgList(treeNode);
			
			//添加区域
			/*List<Long> areaList=areaService.selectByPid(userSession.getAreaId());
			List<Long> areaIdList = new ArrayList<Long>();			
			areaIdList.add(userSession.getAreaId());
			if(null!= areaList){
				areaIdList.addAll(areaList);
			}			
			
			userSession.setAreaIdList(areaIdList);*/
			
			//添加单位
			/*BfCompany BfCompany = iBfCompanyService.selectById(userSession.getCompanyId());
			userSession.setBfCompany(BfCompany);;*/
			
			session.setAttribute("userInfo", userSession);
			modelView.setViewName("redirect:/index.do");
		} else {
			token.clear();
			request.getSession().setAttribute("shiroLoginFailure",
					"userOrPasswdError");
			modelView.setViewName("redirect:/login.do");
		}
		return modelView;

	}

//	@RequestMapping(value = "/adminGetCode", method = RequestMethod.GET)
//	@ResponseBody
//	public String adminGetCode(HttpSession session,HttpServletRequest request, User user) {
//		//根据userName查询出phoneNum发送验证码，并且缓存
//		List<User> userList = userService.selectByExample(user);
//		if (userList != null && userList.size() > 0) {
//			user = userList.get(0);
//		} else {
//			return null;
//		}
//		String code = RandomStringUtils.randomNumeric(6);
//		boolean flag = MsmTool.sendMsm(user.getPhoneNum(), code);
//		if (flag) {
//			session.setAttribute("code", code);
//			session.setMaxInactiveInterval(30*60);//以秒为单位
//		}
//		System.out.println(" ---code---" + code);
//		return "Send success";
//	}
//	
//	@RequestMapping(value = "/adminValiCode", method = RequestMethod.GET)
//	@ResponseBody
//	public Map<String, Object> adminValiCode(HttpSession session,HttpServletRequest request) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		String code = (String)session.getAttribute("code");
//		String reqCode = request.getParameter("code");
//		if (code==null || reqCode==null || !code.equals(reqCode)) {
//			map.put("isError",true);
//		} else {
//			map.put("isError", false);
//		}
//		return map;
//	}

}
